package group

import (
	"api/common"
	"api/pb/group"
	"context"

	"api/internal/svc"
	"api/internal/types"

	"github.com/zeromicro/go-zero/core/logx"
)

type UpdateGroupLogic struct {
	logx.Logger
	ctx    context.Context
	svcCtx *svc.ServiceContext
}

func NewUpdateGroupLogic(ctx context.Context, svcCtx *svc.ServiceContext) *UpdateGroupLogic {
	return &UpdateGroupLogic{
		Logger: logx.WithContext(ctx),
		ctx:    ctx,
		svcCtx: svcCtx,
	}
}

// UpdateGroup 更新群聊信息
func (l *UpdateGroupLogic) UpdateGroup(req *types.GroupInfo) (resp *types.Empty, err error) {
	// 1. 获取操作人表示
	userID, err := common.GetUserIDFromCtx(l.ctx)
	if err != nil {
		return nil, err
	}
	// 2. 更新群聊信息
	_, err = l.svcCtx.GroupRPC.UpdateGroup(l.ctx,
		&group.GroupInfo{Id: req.ID, UserId: userID, Name: req.Name, Avatar: req.Avatar, Desc: req.Desc,
			JoinType: req.JoinType, Status: req.Status,
		})
	if err != nil {
		return nil, err
	}
	return &types.Empty{}, nil
}
